<?php
if ( ! defined('ROOT')) exit('No direct script access allowed');

/**
 * 网站设置
 * @author gzute
 * @link http://www.gzute.com
 */
Base::load_app_class('admin','admin');

class Setting extends Admin
{
	private $form;
	private $rules = array(
           array(
                 'field'   => 'timezone', 
                 'label'   => 'lang:time_zone', 
                 'rules'   => 'required'
              ),
           array(
                 'field'   => 'logo', 
                 'label'   => 'lang:logo', 
                 'rules'   => 'required'
              )	
    );
	
	public function __construct()
	{
		$this->form = Base::load_sys_class('validation');
		$this->form->set_rules($this->rules);
	}
	
	public function settingAction()
	{
		if($_POST){
			
			$config = Base::load_config('config');
			$upload_config = array(
                'upload_path' => ROOT.'data/files',
                'allowed_types' => 'gif|jpg|png',
                'max_size' => '100',
                'overwrite' => TRUE
            );
            $upload = Base::load_sys_class('upload','',1,$upload_config);
            //$upload->initialize($upload_config);
            $upload->do_upload('logo');
            $upload_data = $upload->data();
            //var_dump($upload_data);
            if($upload_data['file_name']){
                $config['logo'] = 'data/files/'.$upload_data['file_name'];
            }    
			foreach($config as $key => $v)
            {
                if(isset($_POST[$key]))
                {
                    $config[$key] = self::post($key,TRUE);
                }
            }
            $config_file =  LIB_PATH.'configs/config.php';
            $data = "<?php\r\nreturn ".var_export($config,true)."; \r\n//end of this file";
            Base::load_sys_func('file');
            write_file($config_file,$data);
            sys_msg(lang('setting_success'));
		}
		
		else {
			self::tpl_display('base_setting');
		}
		
	}
	
	public function clearAction()
    {
        $return['msg'] = 'ok';
        echo json_encode($return);
    }
}